//
//  Sky72CourseViewController.m
//  Sky72
//
//  Created by ladaka on 13. 9. 24..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "Sky72CourseViewController.h"
#import "NaviBarUtil.h"
#import "CDefines.h"

@interface Sky72CourseViewController ()

@end

@implementation Sky72CourseViewController
@synthesize _courseTag;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _isVisitorShow = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"%s [%d]", __FUNCTION__, _courseTag);
    
    self.iCourseScroll.type = iCarouselTypeLinear;
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = 5;
    
    UIImage *image = [UIImage imageNamed:@"a"];
    pagerView.frame = CGRectMake(0.0f, 164.0f, image.size.width*5, image.size.height);
    
    // Pager
//    [pagerView setImage:[UIImage imageNamed:@"a"]
//       highlightedImage:[UIImage imageNamed:@"a-h"]
//                 forKey:@"a"];
//    [pagerView setImage:[UIImage imageNamed:@"b"]
//       highlightedImage:[UIImage imageNamed:@"b-h"]
//                 forKey:@"b"];
//    [pagerView setImage:[UIImage imageNamed:@"c"]
//       highlightedImage:[UIImage imageNamed:@"c-h"]
//                 forKey:@"c"];
//    
//    [pagerView setPattern:@"abcab"];
//    
//    pagerView.center = CGPointMake(160.0f, 164.0f);
//    
//    pagerView.delegate = self;
    
    self.vVisitor.frame = CGRectMake(0, self.view.frame.size.height, self.vVisitor.frame.size.width, self.vVisitor.frame.size.height);
    [self.view addSubview:self.vVisitor];
    
    self.navigationItem.hidesBackButton = YES;
    NaviBarUtil *navBar = [[NaviBarUtil alloc] initWithNavigationController:self.navigationController navigationItem:self.navigationItem];
    [navBar naviBarSetting:@"하늘코스(18H)"];
    [navBar addLeftButtonNormalImage:[UIImage imageNamed:@"head_btn_close"]
                      highlightImage:[UIImage imageNamed:@"head_btn_close_ov"] target:self action:@selector(onClickBack)];
    
    [self settingCourseData];
    [self settingCourseDetail];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setICourseScroll:nil];
    [self setPageControl:nil];
    [self setVCurrentWeather:nil];
    [self setVVisitor:nil];
    [self setLbTitle:nil];
    [self setLbName:nil];
    [self setLbOpen:nil];
    [self setLbLength:nil];
    [self setLbDesign:nil];
    [super viewDidUnload];
}

- (void)onClickBack {
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

- (void)settingCourseData {
    self.arrCourseDetailList = [[NSMutableArray alloc] init];
    
    NSMutableArray *arrSkyList = [[NSMutableArray alloc] init];
    [arrSkyList addObject:SKY_COURSE_NAME];
    [arrSkyList addObject:SKY_COURSE_OPEN];
    [arrSkyList addObject:SKY_COURSE_LENGTH];
    [arrSkyList addObject:SKY_COURSE_DESIGN];
    NSMutableDictionary *arrSkyDicList = [[NSMutableDictionary alloc] init];
    [arrSkyDicList setObject:arrSkyList forKey:@"data"];
    [arrSkyDicList setObject:SKY_COURSE_TITLE forKey:@"title"];
    [self.arrCourseDetailList addObject:arrSkyDicList];
    
    NSMutableArray *arrLakeList = [[NSMutableArray alloc] init];
    [arrLakeList addObject:LAKE_COURSE_NAME];
    [arrLakeList addObject:LAKE_COURSE_OPEN];
    [arrLakeList addObject:LAKE_COURSE_LENGTH];
    [arrLakeList addObject:LAKE_COURSE_DESIGN];
    NSMutableDictionary *arrLakeDicList = [[NSMutableDictionary alloc] init];
    [arrLakeDicList setObject:arrLakeList forKey:@"data"];
    [arrLakeDicList setObject:LAKE_COURSE_TITLE forKey:@"title"];
    [self.arrCourseDetailList addObject:arrLakeDicList];
    
    NSMutableArray *arrClassicList = [[NSMutableArray alloc] init];
    [arrClassicList addObject:SKY_COURSE_NAME];
    [arrClassicList addObject:SKY_COURSE_OPEN];
    [arrClassicList addObject:SKY_COURSE_LENGTH];
    [arrClassicList addObject:SKY_COURSE_DESIGN];
    NSMutableDictionary *arrClassicDicList = [[NSMutableDictionary alloc] init];
    [arrClassicDicList setObject:arrClassicList forKey:@"data"];
    [arrClassicDicList setObject:SKY_COURSE_TITLE forKey:@"title"];
    [self.arrCourseDetailList addObject:arrClassicDicList];
    
    NSMutableArray *arrOceanList = [[NSMutableArray alloc] init];
    [arrOceanList addObject:LAKE_COURSE_NAME];
    [arrOceanList addObject:LAKE_COURSE_OPEN];
    [arrOceanList addObject:LAKE_COURSE_LENGTH];
    [arrOceanList addObject:LAKE_COURSE_DESIGN];
    NSMutableDictionary *arrOceanDicList = [[NSMutableDictionary alloc] init];
    [arrOceanDicList setObject:arrOceanList forKey:@"data"];
    [arrOceanDicList setObject:LAKE_COURSE_TITLE forKey:@"title"];
    [self.arrCourseDetailList addObject:arrOceanDicList];
    
    NSMutableArray *arrDreamList = [[NSMutableArray alloc] init];
    [arrDreamList addObject:SKY_COURSE_NAME];
    [arrDreamList addObject:SKY_COURSE_OPEN];
    [arrDreamList addObject:SKY_COURSE_LENGTH];
    [arrDreamList addObject:SKY_COURSE_DESIGN];
    NSMutableDictionary *arrDreamDicList = [[NSMutableDictionary alloc] init];
    [arrDreamDicList setObject:arrDreamList forKey:@"data"];
    [arrDreamDicList setObject:SKY_COURSE_TITLE forKey:@"title"];
    [self.arrCourseDetailList addObject:arrDreamDicList];
    
    NSMutableArray *arrDreamDunesList = [[NSMutableArray alloc] init];
    [arrDreamDunesList addObject:LAKE_COURSE_NAME];
    [arrDreamDunesList addObject:LAKE_COURSE_OPEN];
    [arrDreamDunesList addObject:LAKE_COURSE_LENGTH];
    [arrDreamDunesList addObject:LAKE_COURSE_DESIGN];
    NSMutableDictionary *arrDreamDunesDicList = [[NSMutableDictionary alloc] init];
    [arrDreamDunesDicList setObject:arrDreamDunesList forKey:@"data"];
    [arrDreamDunesDicList setObject:LAKE_COURSE_TITLE forKey:@"title"];
    [self.arrCourseDetailList addObject:arrDreamDunesDicList];
}

- (void)settingCourseDetail {
    NSMutableArray *arrContentList = [[self.arrCourseDetailList objectAtIndex:_courseTag] objectForKey:@"data"];
    
    NSString *str = [NSString stringWithFormat:@"'%@'",[[self.arrCourseDetailList objectAtIndex:_courseTag] objectForKey:@"title"]];
    
    self.lbTitle.text = str; //[[self.arrCourseDetailList objectAtIndex:_courseTag] objectForKey:@"title"];
    
    self.lbName.text = [arrContentList objectAtIndex:0];
    self.lbOpen.text = [arrContentList objectAtIndex:1];
    self.lbLength.text = [arrContentList objectAtIndex:2];
    self.lbDesign.text = [arrContentList objectAtIndex:3];
}

#pragma mark - onClick
- (IBAction)onClickCoursePrev:(id)sender {
    [self.iCourseScroll scrollToItemAtIndex:[self.iCourseScroll currentItemIndex] - 1 animated:YES];
}

- (IBAction)onClickCourseNext:(id)sender {
    [self.iCourseScroll scrollToItemAtIndex:[self.iCourseScroll currentItemIndex] + 1 animated:YES];
}

- (IBAction)onClickVisitor:(id)sender {
    if (_isVisitorShow == NO)
    {
        [UIView beginAnimations:@"showVisitorView" context:NULL];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        CGRect f = self.vVisitor.frame;
        
        CGFloat height = self.view.frame.size.height;
        CGFloat vVisitorHeight = self.vVisitor.frame.size.height;
        self.vVisitor.frame = CGRectMake(0, height - vVisitorHeight, f.size.width, f.size.height);
        
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        _isVisitorShow = YES;
    } else {
        [UIView beginAnimations:@"showVisitorView" context:NULL];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        CGRect f = self.vVisitor.frame;
        
        CGFloat height = self.view.frame.size.height;
        self.vVisitor.frame = CGRectMake(0, height, f.size.width, f.size.height);
        
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        _isVisitorShow = NO;
    }
}

- (IBAction)onClickPageControl:(id)sender {
    int page = self.pageControl.currentPage;
    NSLog(@"%s", __FUNCTION__);
//    UIPageControl *pager = sender;
//    int page = pager.currentPage;
//    
    [self.iCourseScroll scrollToItemAtIndex:page animated:YES];
}

#pragma mark - iCarousel delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return 5;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
    
    UIImageView *itemView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_businessinfo3_ov.png"]];
    itemView.frame = CGRectMake(0, 0, 320, 200);
                            
    
//        if (itemView == nil) {
//            itemView.delegate = self;
//        }
//        
//        itemView.alpha = 0;
//        
//        if(communicationCafeList != nil && [communicationCafeList count] > 0 && [communicationCafeList count] >= index){
//            ContentObj *obj = [communicationCafeList objectAtIndex:index];
//            if(obj != nil){
//                [itemView setUsrId:obj.ownerId];
//                [itemView setTitle:obj.subJect];
//                [itemView setDescription:obj.description];
//                
//                // Configure the cell...
//                
//                [ImageUtil loadAsyncImageFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MAIN_URL, obj.imgUrl]] imageBlock:^(UIImage *image){
//                    [itemView setImage:image];
//                    
//                    [UIView beginAnimations:@"alphaAnimation" context:nil];
//                    [UIView setAnimationDuration:0.3];
//                    [UIView setAnimationDelegate:self];
//                    itemView.ivItem.alpha = 1;
//                    [UIView commitAnimations];
//                }
//                                      errorBlock:^(void){
//                                          [itemView setImage:[UIImage imageNamed:@"noimg_master.png"]];
//                                          
//                                          [UIView beginAnimations:@"alphaAnimation" context:nil];
//                                          [UIView setAnimationDuration:0.3];
//                                          [UIView setAnimationDelegate:self];
//                                          itemView.ivItem.alpha = 1;
//                                          [UIView commitAnimations];
//                                      }];
//                
//                itemView.content = obj;
//            }
//        }
    
        return itemView;
}


- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * _carousel.itemWidth);
}

- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return YES;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (_carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        default:
        {
            return value;
        }
    }
}

- (void) carouselDidEndScrollingAnimation:(iCarousel *)carousel {
    self.pageControl.currentPage = [carousel currentItemIndex];
//    pagerView.page = [carousel currentItemIndex];
}

#pragma mark - MCPagerViewDelegate
//- (void)pageView:(MCPagerView *)pageView didUpdateToPage:(NSInteger)newPage
//{
//    [self.iCourseScroll scrollToItemAtIndex:pagerView.page animated:YES];
//}
@end
